const Controller = require('egg').Controller
// const fs = require('mz/fs');
const fs = require('fs')
const path = require('path')
const pump = require('mz-modules/pump')

class UploadController extends Controller {

  async handle() {
    const {ctx} = this
    // let files = ctx.request.files
    // console.log(files);
    let file = await ctx.request.files[0]
    // console.log(file);
    let filepath = fs.readFileSync(file.filepath) 
    // console.log(filepath);
    // console.log(path.extname(file.filename).toLowerCase());
    // console.log(this.config.baseDir);
    const filename = new Date().getTime() + path.extname(file.filename).toLowerCase()
    const target = path.join(this.config.baseDir,'app/public/uploads',filename)
     fs.writeFileSync(target, filepath)
    // console.log(fs.readFileSync(target));
    console.log(target);
    ctx.body = {
      code:20000,
      data: `/uploads/${filename}`
    }

    // const stream = await this.ctx.getFileStream();
    //     const filename = new Date().getTime() + path.extname(stream.filename).toLowerCase();
    //     const target = path.join(this.config.baseDir, 'app/public/uploads', filename);
    //     const writeStream = fs.createWriteStream(target);
    //     await pump(stream, writeStream);
    //     this.ctx.body = {
    //         code: 20000,
    //         data: {
    //             name: filename,
    //             file: `/uploads/${filename}` //正式地址
    //             // file: `http://127.0.0.1:7001/uploads/${filename}`  //临时服务器地址
    //         }
    //     }
    //     console.log(this.ctx.body)
    
  }

}

module.exports = UploadController